package com.zpark.resume.controller;

import com.zpark.resume.common.ResponseResult;
import com.zpark.resume.common.StatusCode;
import com.zpark.resume.entity.Cv;
import com.zpark.resume.entity.Member;
import com.zpark.resume.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @PackageName: com.zpark.resume.controller
 * @ClassName: MemberController
 * @Description: 会员控制器
 * @author: RZS
 * @date: 2023/5/30  16:06
 */
@RestController
@CrossOrigin
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;

    @PostMapping("/login")
    public ResponseResult<Member> login(@RequestBody Member member, HttpSession session){
        // 1、调用业务层执行登录
        Member member1 = memberService.login(member.getUsname(), member.getPsword());
        // 2、将登录信息绑定到session
        session.setAttribute("member", member1);
        // 3、响应数据
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), member1);
    }

    /**
     * 用户退出登录功能
     * @param session
     * @return
     */
    @GetMapping("/login_out")
    public ResponseResult<Void> loginOut(HttpSession session){
        // 清除session数据
        session.invalidate();

        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), null);
    }

    /**
     * 会员注册
     * @param member
     * @return
     */
    @PostMapping("/reg")
    public ResponseResult<Void> reg(@RequestBody Member member){
        // 调用业务层执行注册功能
        memberService.reg(member);
        // 返回数据
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), null);
    }

    /**
     * 根据id查询member数据
     * @param id id
     * @return
     */
    @PostMapping("/find_by_id/{id}")
    public ResponseResult<Member> findMemberById(@PathVariable("id") Integer id){
        // 调用业务层接口查询数据
        Member member = memberService.findById(id);
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), member);
    }
}
